package com.csun.cmny.provider.service.impl;

import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcLifeTransducerDefaultConfigMapper;
import com.csun.cmny.provider.model.constant.LifeConstant;
import com.csun.cmny.provider.model.domain.DmcLifeTransducerDefaultConfig;
import com.csun.cmny.provider.model.dto.life.LifeTransducerConfigDto;
import com.csun.cmny.provider.model.vo.life.LifeTransducerConfigVO;
import com.csun.cmny.provider.service.DmcLifeTransducerDefaultConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Slf4j
@Service
@Transactional
public class DmcLifeTransducerDefaultConfigServiceImpl extends BaseService<DmcLifeTransducerDefaultConfig> implements DmcLifeTransducerDefaultConfigService {
    @Resource
    private DmcLifeTransducerDefaultConfigMapper dmcLifeTransducerDefaultConfigMapper;

    @Override
    public LifeTransducerConfigVO getDefaultConfigByGroupId(Long groupId) {
        //查询机构默认配置
        DmcLifeTransducerDefaultConfig dmcLifeTransducerConfig = dmcLifeTransducerDefaultConfigMapper.selectByPrimaryKey(groupId);
        //如果不存在,则添加一条默认数据
        if(dmcLifeTransducerConfig == null){
            dmcLifeTransducerConfig = new DmcLifeTransducerDefaultConfig();
//            dmcLifeTransducerConfig.setLifeRoomId(LifeConstant.DEFAULT_LIFE_DEFAULT_ROOM_CONFIG_ID);
            dmcLifeTransducerConfig.setGroupId(groupId);

            dmcLifeTransducerConfig.setToiletAttentionTime(LifeConstant.DEFAULT_TOILET_ATTENTION_TIME);
            dmcLifeTransducerConfig.setIsUseToiletAttentionTime(LifeConstant.DEFAULT_IS_USE_TOILET_ATTENTION_TIME);
            dmcLifeTransducerConfig.setToiletWarningTime(LifeConstant.DEFAULT_TOILET_WARNING_TIME);
            dmcLifeTransducerConfig.setIsUseToiletWarningTime(LifeConstant.DEFAULT_IS_USE_TOILET_WARNING_TIME);
            dmcLifeTransducerConfig.setToiletUnusualTime(LifeConstant.DEFAULT_TOILET_UNUSUAL_TIME);
            dmcLifeTransducerConfig.setIsUseToiletUnusualTime(LifeConstant.DEFAULT_IS_USE_TOILET_UNUSUAL_TIME);
            dmcLifeTransducerConfig.setTemperatureMaximum(LifeConstant.DEFAULT_TEMPERATURE_MAXIMUM);
            dmcLifeTransducerConfig.setTemperatureMinimum(LifeConstant.DEFAULT_TEMPERATURE_MINIMUM);
            dmcLifeTransducerConfig.setTemperatureMeasuringTimeInterval(LifeConstant.DEFAULT_TEMPERATURE_MEASURING_TIME_INTERVAL);
            dmcLifeTransducerConfig.setBreathMaxTimes(LifeConstant.DEFAULT_BREATH_MAX_TIMES);
            dmcLifeTransducerConfig.setBreathMinTimes(LifeConstant.DEFAULT_BREATH_MIN_TIMES);
            dmcLifeTransducerConfig.setHeartbeatMaximum(LifeConstant.DEFAULT_HEARTBEAT_MAXIMUM);
            dmcLifeTransducerConfig.setHeartbeatMinimum(LifeConstant.DEFAULT_HEARTBEAT_MINIMUM);
            dmcLifeTransducerConfig.setVitalMeasuringTimeInterval(LifeConstant.DEFAULT_VITAL_MEASURING_TIME_INTERVAL);
            dmcLifeTransducerConfig.setLeaveBedMeasuringTimeInterval(LifeConstant.DEFAULT_LEAVE_BED_MEASURING_TIME_INTERVAL);
            dmcLifeTransducerConfig.setWalkMeasuringStartTime(LifeConstant.DEFAULT_WALK_MEASURING_START_TIME);
            dmcLifeTransducerConfig.setWalkMeasuringEndTime(LifeConstant.DEFAULT_WALK_MEASURING_END_TIME);
            dmcLifeTransducerConfig.setWalkMeasuringTimeInterval(LifeConstant.DEFAULT_WALK_MEASURING_TIME_INTERVAL);
            dmcLifeTransducerConfig.setTumbleMeasuringFrame(LifeConstant.DEFAULT_TUMBLE_MEASURING_FRAME);
            dmcLifeTransducerConfig.setCameraCheckTime(LifeConstant.CAMERA_CHECK_TIME);
            dmcLifeTransducerConfig.setBeforeDuration(LifeConstant.BEFORE_DURATION);
            dmcLifeTransducerConfig.setAfterDuration(LifeConstant.AFTER_DURATION);
            dmcLifeTransducerConfig.setLeaveRoomAlertStartTime(LifeConstant.DEFAULT_LEAVE_ROOM_ALERT_START_TIME);
            dmcLifeTransducerConfig.setLeaveRoomAlertEndTime(LifeConstant.DEFAULT_LEAVE_ROOM_ALERT_END_TIME);
            dmcLifeTransducerDefaultConfigMapper.insertSelective(dmcLifeTransducerConfig);
        }
        LifeTransducerConfigVO vo = new LifeTransducerConfigVO();
        BeanUtils.copyProperties(dmcLifeTransducerConfig,vo);

        return vo;
    }

    @Override
    public Integer editGroupDefaultConfig(LifeTransducerConfigDto dto) {
        DmcLifeTransducerDefaultConfig dmcLifeTransducerConfig = new DmcLifeTransducerDefaultConfig();
        BeanUtils.copyProperties(dto,dmcLifeTransducerConfig);

        return dmcLifeTransducerDefaultConfigMapper.updateByPrimaryKeySelective(dmcLifeTransducerConfig);
    }
}
